import { SvgIcon } from '@/components/icon';
import { TreeData } from '@/types/dataset';

const iconSize = 24;
const iconMap: Record<string, string> = {
  md: 'bxs--file-md',
  txt: 'bxs--file-txt',
  pdf: 'bxs--file-pdf',
  ppt: 'ant-design--file-ppt-filled',
  pptx: 'ant-design--file-pptx-filled',
  xls: 'teenyicons--xls-solid',
  xlsx: 'teenyicons--xlsx-solid',
  csv: 'fa-solid--file-csv',
  doc: 'bxs--file-doc',
  docx: 'bxs--file-docx',
  file: 'bxs--file',
  gif: 'bxs--file-gif',
  jpg: 'bxs--file-jpg',
};

// const copyIconMap: Record<string, string> = {
//   md: 'bxs--file-md-copy',
//   txt: 'bxs--file-txt-copy',
//   pdf: 'bxs--file-pdf-copy',
//   ppt: 'ant-design--file-ppt-filled-copy',
//   pptx: 'ant-design--file-pptx-filled-copy',
//   xls: 'teenyicons--xls-solid-copy',
//   xlsx: 'teenyicons--xlsx-solid-copy',
//   csv: 'fa-solid--file-csv-copy',
//   doc: 'bxs--file-doc-copy',
//   docx: 'bxs--file-docx-copy',
//   file: 'bxs--file',
//   gif: 'bxs--file-gif-copy',
//   jpg: 'bxs--file-jpg-copy',
// };

// 获取文件名后缀，可传入 SvgIcon 参数
type SvgIconOptions = { className?: string; size?: number; color?: string };
const getFileIcon = (name: string, options?: SvgIconOptions) => {
  const { className, size = iconSize, color = '#9e69fa' } = options || {};
  try {
    const suffix = name.split('.').pop()?.toLowerCase() || '';
    const iconKey = suffix in iconMap ? suffix : 'file';
    return (
      <SvgIcon
        color={className ? '' : color}
        className={`mb-1 ml-1 mr-1 mt-1 ${className || ''}`}
        icon={iconMap[iconKey]}
        size={size}
      />
    );
  } catch {
    return (
      <SvgIcon
        color={className ? '' : color}
        className={`mb-1 ml-1 mr-1 mt-1 ${className || ''}`}
        icon={iconMap.file}
        size={size}
      />
    );
  }
};

export const getDatasetIcon = (
  node: TreeData,
  expandedKeys: string[],
  detail: boolean,
  className?: string,
) => {
  const { id, type, name } = node;
  const open = expandedKeys.includes(id || '');

  const baseClass = `mb-1 ml-1 mr-1 mt-1 ${className || ''}`;
  const withColor = (color: string) => (className ? '' : color);

  if (type === 'folder') {
    const folderColor = '#FFCA28';
    return (
      <SvgIcon
        color={withColor(folderColor)}
        className={baseClass}
        icon={open ? 'ri--folder-open-fill' : 'ri--folder-2-fill'}
        size={24}
      />
    );
  }

  if (type === 'file') {
    return getFileIcon(name || '', { className });
  }

  if (type === 'dataset') {
    return (
      <SvgIcon
        color={className ? '' : '#3F70FF'}
        className={`m-1 ${className || ''}`}
        icon="mdi--folder-zip"
        size={24}
      />
    );
  }

  const linkLikeTypes = new Set(['link', 'img', 'video']);
  if (linkLikeTypes.has(type || '')) {
    return <SvgIcon color={withColor('#BF63FF')} className={baseClass} icon="ic-link" size={24} />;
  }

  return <SvgIcon color={withColor('#BF63FF')} className={baseClass} icon="bxs--file" size={24} />;
};
